package allwe.thread05;

/**
 * 生产者线程
 */
public class ProducerThread extends Thread{
    //共享商品
    private Product p;

    public ProducerThread(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        //生产10个商品
        for (int i = 0; i < 10; i++) {
            synchronized (p){
                if (i % 2 == 0){
                    //生产费列罗巧克力
                    p.setBrand("费列罗");
                    try {
                        Thread.sleep(100);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    p.setName("巧克力");
                }else {
                    //哈尔滨啤酒
                    p.setBrand("哈尔滨");
                    try {
                        Thread.sleep(100);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    p.setName("啤酒");
                }
                System.out.println("生产者生产了" + p.getBrand() + "-------------" + p.getName());
            }
        }
    }
}
